package com.avalon.shop.service;

import com.avalon.core.condition.Condition;
import com.avalon.core.model.RecordRow;
import com.avalon.core.util.ObjectUtils;
import com.avalon.core.util.StringUtils;
import com.avalon.shop.ShopContext;
import com.avalon.shop.config.ShopConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PetAdoptService extends BaseService {
    private final WxCheckRecordService wxCheckRecordService;

    public PetAdoptService(ShopConfig shopConfig, ShopContext shopContext,
                           @Lazy WxCheckRecordService wxCheckRecordService) {
        super(shopConfig, shopContext);
        this.wxCheckRecordService = wxCheckRecordService;
    }

    @Override
    protected String getServiceName() {
        return "pet.adopt";
    }

    public void publishPetAdopt(String openId,
                                Integer petId,
                                String selfDescription,
                                Integer publishId,
                                String avatar,
                                String video) {

        Map<String, Object> param = new HashMap<>();
        param.put("petId", petId);
        param.put("selfDescription", selfDescription);
        param.put("publishId", publishId);
        param.put("avatar", avatar);
        param.put("video", video);
        RecordRow recordRow = addModel(param);

        if (StringUtils.isNotEmpty(avatar)) {
            wxCheckRecordService.createAdoptCheckRecord(recordRow.getInteger("id"), openId, avatar);
        }
    }

    public List<Map> getAdoptList(Integer pageNum, Integer pageSize, String searchName, String checkState, Integer userId) {
        String rpnCondition = "";
        Condition condition = null;
        if (StringUtils.isNotEmpty(searchName)) {
            condition = Condition.likeCondition("selfDescription", searchName);
        }
        if (StringUtils.isNotEmpty(checkState)) {
            if (ObjectUtils.isNotNull(condition)) {
                condition = condition.andEqualCondition("checkState", checkState);
            } else {
                condition = Condition.equalCondition("checkState", checkState);
            }
        }
        if (ObjectUtils.isNotNull(userId)) {
            if (ObjectUtils.isNotNull(condition)) {
                condition = condition.andEqualCondition("publishId", userId);
            } else {
                condition = Condition.equalCondition("publishId", userId);
            }
        }
        if (ObjectUtils.isNotNull(condition)) {
            rpnCondition = condition.getReversePolishNotation();
        }
        return getModelPage("id,avatar,state,selfDescription," +
                        "createTime,publishId.id," +
                        "publishId.nickName,publishId.avatar",
                pageNum,
                pageSize,
                rpnCondition,
                "createTime desc, id desc");
    }

    public Map getAdoptDetail(Integer id) {
        String fields = "createTime,petId.id,petId.name,petId.petSmallTypeId.name,petId.petCategoryId.name"
                + ",selfDescription,avatar,state,video,publishId.id,publishId.nickName,publishId.avatar"
                + ",petId.sex,petId.birthday,petId.avatar,petId.weight,petId.sterilization"
                + ",petId.dewormIds.dewormDate,petId.dewormIds.dewormKind"
                + ",petId.vaccinationIds.vaccinationDate,petId.vaccinationIds.vaccinationName";
        return getModelDetail(fields, id);
    }

    private final String adoptMessageService = "pet.adopt.message";

    public RecordRow insertAdoptMessage(Integer petAdoptId,
                                        Integer petUserId,
                                        String message) {
        Map<String, Object> model = new HashMap<>();
        model.put("petAdoptId", petAdoptId);
        model.put("message", message);
        model.put("petUserId", petUserId);
        return addModel(adoptMessageService, model);
    }

    private String fields = "id,petAdoptId,message,createTime," +
            "petUserId.id,petUserId.avatar,petUserId.nickName";

    public Map getAdoptMessage(Integer id) {
        return getModelDetail(adoptMessageService, fields, id);
    }

    public Integer getAdoptMessageCount(Integer petAdoptId) {
        String condition = "(=,petAdoptId," + petAdoptId + ")";
        return getAllModel(adoptMessageService, "id", condition, "").size();
    }

    public List<Map> getAdoptMessage(Integer pageNum, Integer pageSize, Integer petAdoptId) {
        String condition = "(=,petAdoptId," + petAdoptId + ")";
        return getModelPage(adoptMessageService, fields, pageNum,
                pageSize, condition, "createTime desc");
    }

    public void updateCheckFailState(Integer adoptId) {
        Map<String, Object> update = new HashMap<>();
        update.put("id", adoptId);
        update.put("checkState", "fail");
        updateModel(update);
    }

    public void checkAdoptState(Integer adoptId, String state) {
        Map<String, Object> update = new HashMap<>();
        update.put("id", adoptId);
        update.put("checkState", state);
        updateModel(update);
    }
}
